package com.lyd.sweet.web.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger2配置文件，集成swagger，通过配置可设置swagger是否开启，标题，扫描路径等
 * 配置示例
 * sweet:
 *   api-doc:
 *     enabled: true
 *     title: api文档
 *     description: 文档描述
 *     basePackage: com.biz.xxx
 *
 * @author 木木
 */
@EnableSwagger2
@ConditionalOnProperty(prefix = "sweet.api-doc", name = "enabled", havingValue = "true")
@ConfigurationProperties(prefix = "sweet.api-doc")
public class SwaggerConfig implements WebMvcConfigurer {
    private static Logger logger = LoggerFactory.getLogger(SwaggerConfig.class);

    /**
     * 是否启用api 文档，如swagger
     */
    private boolean enabled = false;

    /**
     * api文档标题
     */
    private String title = "sweet-web-api";

    /**
     * api文档描述
     */
    private String description = "api文档描述";

    /**
     * 自动扫描文档注解的根路径
     */
    private String basePackage = "com.lyd";

    /**
     * api版本
     */
    private String version = "1.0.0";

    @Bean
    public Docket createRestApi() {
        logger.info("sweet swagger ApiDoc Init {}", enabled);
        return new Docket(DocumentationType.SWAGGER_2)
                //正式服务器设置为false
                .enable(enabled)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (enabled) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title + " Restful API")
                .description(description)
                .contact(new Contact("木木", "http://localhost", ""))
                .version(version)
                .build();

    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}
